﻿Imports System.Data.SqlClient
Imports System.Collections
Imports System.Text
Imports Microsoft.VisualBasic
Imports System.Windows
Imports System.IO
Imports System.Xml
Imports System.Xml.Linq
Imports System.Xml.XPath
Imports System.Linq
Imports System.Data

Public Class Base
	Private Shared _sqlConn As New Generic.Dictionary(Of Char, SqlConnection)
	Private Shared sbConsole As New StringBuilder
	Public Shared bQuickMode As Boolean = False
	Public Shared win As Window

	Public Shared Property sqlConn(ByVal c As Char) As SqlConnection
		Get
			If _sqlConn.ContainsKey(c) Then
				Return _sqlConn(c)
			Else
				Return Nothing
			End If
		End Get
		Set(ByVal value As SqlConnection)
			If _sqlConn.ContainsKey(c) Then
				_sqlConn(c) = value
			Else
				_sqlConn.Add(c, value)
			End If
		End Set
	End Property

	Public Delegate Sub WriteToConsoleCallback(ByVal str As String, ByVal bMessageBox As Boolean)
	Public Shared Sub WriteToConsole(ByVal str As String, Optional ByVal bMessageBox As Boolean = False)
		sbConsole.Append(str)
		sbConsole.Append(vbCrLf)
		If win.GetType() Is GetType(Window1) Then
			DirectCast(win, Window1).txtConsole.Text = sbConsole.ToString
			DirectCast(win, Window1).txtConsole.ScrollToEnd()
		End If

		If bMessageBox Then
			MsgBox(str)
		End If
	End Sub

	Public Shared Sub ReOpenConnections()
		If Base.sqlConn(CChar("A")) IsNot Nothing AndAlso Base.sqlConn(CChar("B")) IsNot Nothing Then
			If Not Base.sqlConn(CChar("A")).State = ConnectionState.Open OrElse Not Base.sqlConn(CChar("B")).State = ConnectionState.Open Then
				If Base.sqlConn(CChar("A")).Equals(Base.sqlConn(CChar("B"))) Then
					Base.sqlConn(CChar("A")).Open()
					Base.sqlConn(CChar("B")) = Base.sqlConn(CChar("A"))
				Else
					If Not Base.sqlConn(CChar("A")).State = ConnectionState.Open Then
						Base.sqlConn(CChar("A")).Open()
					End If
					If Not Base.sqlConn(CChar("B")).State = ConnectionState.Open Then
						Base.sqlConn(CChar("B")).Open()
					End If
				End If
			End If
		End If
	End Sub

	Public Shared docConnections As XDocument

	Public Shared Sub GetConnections()
		If File.Exists("connections.xml") Then
			docConnections = XDocument.Load("connections.xml")
		Else
			docConnections = Nothing
		End If
	End Sub

	Public Shared Sub SaveConnection(ByVal strServerType As String, ByVal strServerName As String, ByVal strAuthentication As String, ByVal strUsername As String)
		Try
			If docConnections Is Nothing Then
				docConnections = New XDocument()
				Dim xConnections As New XElement("Connections")
				docConnections.Add(xConnections)
			End If

			Dim xOldConnection As XElement = docConnections.XPathSelectElement("/Connections/Connection[Server='" & strServerName & "']")
			If xOldConnection IsNot Nothing Then
				xOldConnection.Remove()
			End If

			Dim xConnection As XElement = _
						New XElement("Connection", _
							 New XElement("ServerType", strServerType), _
							 New XElement("Server", strServerName), _
							 New XElement("Authentication", strAuthentication), _
							 New XElement("Username", strUsername) _
						)
			docConnections.Root.AddFirst(xConnection)

			docConnections.Save("connections.xml")
		Catch ex As Exception
			Base.WriteToConsole(ex.ToString)
		End Try
	End Sub

	Public Shared Function TableForQuery(ByVal TableName As String) As String
		Return TableName.Replace("]", "]]")
	End Function

	Public Shared Function NotNull(ByVal value As Object, Optional ByVal defaultValue As Object = "") As Object
		If value Is Nothing Then
			Return defaultValue
		Else
			Return value
		End If
	End Function

End Class
